package com.sie.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sie.entity.Authority;
import org.apache.ibatis.annotations.*;
import org.mockito.internal.matchers.Find;

import java.util.List;

/**
 * 权限持久层
 */
@Mapper
public interface AuthorityMapper extends BaseMapper<Authority> {

    @Select("select * from authority where auth_id = #{id}")
    Authority findById(String id);

    List<Authority> findByPage(Authority authority);

    @Delete("delete from authority where auth_id=#{id}")
    Integer deleteAuthorityById(String id);

    @Update("update authority set auth_name=#{authName},auth_no=#{authNo},auth_lv=#{authLv},auth_parent_id=#{authParentId},auth_status=#{authStatus},auth_path=#{authPath},auth_callout=#{authCallout} where auth_id=#{authId}")
    Integer updateAuthority(Authority authority);

    @Select("select * from authority where auth_name=#{name}")
    Authority findByName(String name);

    @Insert("insert into role_authority (role_id,authority_id) values (#{roleId},#{authorityId})")
    int setAuthority(@Param("roleId") String roleId, @Param("authorityId") String id);

    @Select("select a.* from authority a left join role_authority r on a.auth_id=r.authority_id where r.role_id=#{id}")
    List<Authority> findByRoleId(String id);

    @Select("select * from authority where auth_id=#{id}")
    List<Authority> FindSons(String parentId);

    @Select("select * from authority where auth_parent_id=#{id}")
    List<Authority> FindFather(String parentId);

    @Delete("delete from role_authority where authority_id=#{id}")
    int deleteAuthorityAndRole(String id);

    @Select("select * from authority where auth_lv=#{lv}")
    List<Authority> findAuthorityByLv(String lv);

    @Delete("delete from role_authority where role_id=#{roleId} ")
    int deleteAllAuthByRoleId(String roleId);

    @Select("select authority.*\n" +
            "from authority\n" +
            "         left join (select role.role_id role_id,ra.authority_id auth_id  from role left join role_authority ra on role.role_id = ra.role_id) te\n" +
            "                   on authority.auth_id = te.auth_id\n" +
            "where te.role_id = #{roleId}")
    List<Authority> findAuthByRoleId(String roleId);
}
